package com.chatplus.application.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chatplus.application.common.exception.BadRequestException;
import com.chatplus.application.common.logging.SouthernQuietLogger;
import com.chatplus.application.common.logging.SouthernQuietLoggerFactory;
import com.chatplus.application.domain.entity.draw.SdJobEntity;
import com.chatplus.application.domain.request.ImagePublishRequest;
import com.chatplus.application.domain.request.SdImageJobRequest;
import com.chatplus.application.domain.response.SdJobDetailApiResponse;
import com.chatplus.application.service.draw.SdJobService;
import com.chatplus.application.web.basecontroller.BaseController;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SD API
 *
 * @Author: Angus
 */
@Validated
@RestController
@RequestMapping("/api/sd")
@Tag(name = "产品API", description = "SD API")
public class SdJobApiController extends BaseController {

    private static final SouthernQuietLogger LOGGER = SouthernQuietLoggerFactory.getLogger(SdJobApiController.class);

    private final SdJobService sdJobService;

    public SdJobApiController(SdJobService sdJobService) {
        this.sdJobService = sdJobService;
    }

    /**
     * 获取任务列表或者创作记录
     *
     * @param status 状态 0 就是不成功的，1 就是成功的
     * @param userId 用户ID
     * @return 任务列表
     */
    @GetMapping("/jobs")
    @Operation(summary = "获取任务列表或者创作记录")
    public List<SdJobDetailApiResponse> jobs(@RequestParam String status,
                                             @RequestParam(value = "user_id", required = false) Long userId,
                                             @RequestParam(value = "page", required = false) Long page,
                                             @RequestParam(value = "page_size", required = false) Long pageSize,
                                             @RequestParam(value = "publish", required = false) Boolean publish) {
        if ("0".equals(status)) {
            // 获取失败的任务
            List<SdJobEntity> sdJobList = sdJobService.list(new LambdaQueryWrapper<SdJobEntity>()
                    .eq(SdJobEntity::getUserId, userId)
                    .ne(SdJobEntity::getProgress, -1)
                    .lt(SdJobEntity::getProgress, 100)
                    .orderByDesc(SdJobEntity::getCreatedAt));
            return sdJobList.stream().map(SdJobDetailApiResponse::build).toList();
        } else if ("1".equals(status)) {
            // 获取成功的任务
            List<SdJobEntity> sdJobList = sdJobService.list(new LambdaQueryWrapper<SdJobEntity>()
                    .eq(SdJobEntity::getUserId, userId)
                    .eq(SdJobEntity::getProgress, 100)
                    .orderByDesc(SdJobEntity::getCreatedAt));
            return sdJobList.stream().map(SdJobDetailApiResponse::build).toList();
        }
        return Lists.newArrayList();
    }

    /**
     * 画图任务
     */
    @PostMapping("/image")
    @Operation(summary = "画图任务")
    public void image(@RequestBody SdImageJobRequest request) {
        throw new BadRequestException("暂未重构完成，敬请期待");
    }

    /**
     * 删除图片
     */
    @PostMapping("/remove")
    @Operation(summary = "删除图片")
    public void remove(@RequestBody ImagePublishRequest request) {
        sdJobService.removeById(request.getId());
    }

    /**
     * 发布图片
     */
    @PostMapping("/publish")
    @Operation(summary = "发布图片")
    public void publish(@RequestBody ImagePublishRequest request) {
        sdJobService.update(new LambdaUpdateWrapper<SdJobEntity>()
                .set(SdJobEntity::getPublish, request.isAction())
                .eq(SdJobEntity::getId, request.getId()));
    }

}
